package com.youyi.dutyChain;

import java.util.List;

/**执行责任链工厂
 * @author youyi.time
 * @date 2024/11/23 0:11
 */
public class DutyChainFactory<T,R> {

    IHandler<T,R> first;


    public DutyChainFactory(List<IHandler> handleList){
        if(handleList == null) throw new NullPointerException("dutyChiaHandles is null!");
        if(handleList.size() == 0) {
            System.out.println("You have no processing to execute!");
            return;
        }
        this.first = handleList.get(0);
        IHandler temp = first;
        //设置责任链
        for(int ii = 1 ; ii < handleList.size() ; ii++){
            temp.setNextHandler(handleList.get(ii));
            temp = temp.getNextHandler();
        }
        temp.setNextHandler(null);
    }

    public R executeHandle(T t){
        if(first!= null) return first.handle(t);
        return null;

    }
}
